package com.bdqn.BookManage.servlet;

import com.bdqn.BookManage.biz.BookInfoBiz;
import com.bdqn.BookManage.biz.OrderBiz;
import com.bdqn.BookManage.biz.OrderGoodsBiz;
import com.bdqn.BookManage.biz.OrderSettlementsBiz;
import com.bdqn.BookManage.biz.impl.BookInfoBizImpl;
import com.bdqn.BookManage.biz.impl.OrderBizImpl;
import com.bdqn.BookManage.biz.impl.OrderGoodsBizImpl;
import com.bdqn.BookManage.biz.impl.OrderSettlementsBizImpl;
import com.bdqn.BookManage.po.BookInfo;
import com.bdqn.BookManage.po.Order;
import com.bdqn.BookManage.po.OrderGoods;
import com.bdqn.BookManage.po.OrderSettlements;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author : CP
 * @version: 1.0
 * @program : BookManage
 * @description : 修改订单的状态
 * @date : 2020-08-01 16:12
 **/
@WebServlet("/zfb")
public class AmendOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        /** 获取业务层对象 */
        BookInfoBiz bookInfoBiz = new BookInfoBizImpl();
        OrderBiz orderBiz = new OrderBizImpl();
        OrderGoodsBiz orderGoodsBiz = new OrderGoodsBizImpl();
        OrderSettlementsBiz orderSettlementsBiz = new OrderSettlementsBizImpl();


        /** 获取订单编号 */
        String out_trade_no = req.getParameter("out_trade_no");
        /** 获取交易金额 */
        Double total_amout = Double.valueOf(req.getParameter("total_amout"));
        /** 在线交易支付流水编号 */
        String trade_no = req.getParameter("trade_no");
        /** 交易时间 */
        String timestamp = req.getParameter("timestamp");

        /** 时间转换 */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(timestamp);
        } catch (Exception e) {
            e.printStackTrace();
        }

        /** 声明订单信息 */
        Order order = orderBiz.queryByNo(out_trade_no);
        /** 修改订单状态(是否支付 0未付 1已付) */
        order.setIsPay(1);
        /** 修改订单实付金额 */
        order.setRealTotalMoney(total_amout);
        /** 修改订单所得积分 (消费10元获得1积分)*/
        order.setOrderScore((int) (total_amout%10));
        /** 进行修改 */
        orderBiz.amend(order);

        /** 修改订单结算数据 */
        OrderSettlements orderSettlements = orderSettlementsBiz.queryById(out_trade_no);
        /** 实际支付金额 */
        orderSettlements.setOrderMoney(total_amout);
        /** 订单状态 */
        orderSettlements.setIsFinish(1);
        /** 结算时间 */
        orderSettlements.setFinishTime(date);
        /** 进行修改 */
        orderSettlementsBiz.amend(orderSettlements);

        /** 修改商品库存 */
        OrderGoods orderGoods = orderGoodsBiz.queryByIo(out_trade_no);
        BookInfo bookInfo = bookInfoBiz.querBookInfo(orderGoods.getGoodsId());
        bookInfo.setStock(bookInfo.getStock()-orderGoods.getGoodsNums());
        bookInfoBiz.updateById(bookInfo);
        /** 修改用户实际得到的积分 */
        /** 重新查询该订单信息，将信息发送到付款成功页面 */


    }
}
